<?php

namespace Home\Controller;
use OT\DataDictionary;

class CateController extends HomeController {
    //系统首页
    public function catalog_grid($fid){
        $nickname       =   I('nickname');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['id|title']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$nickname.'%');
        }

        $list   = $this->lists('shop',$map,'id');
        //$img   = $this->lists('price',$map,'id');
        
        $waitList = array(); //等待堆栈（数组用做堆栈），未处理的id
                array_push($waitList, $fid); //我这是点中某个节点（cid），列出所有子类中的数据
                $rsList = array(); //结果队列

                while (count($waitList) > 0) {
                    //等待堆栈中还有节点，继续处理
                    $tmp = array_pop($waitList); //取出一个节点
                    array_push($rsList, $tmp); //输出这个节点

                    $dc          = M("cate"); //从数据表中找出这个节点的所有子节点
                    $dcon['fid'] = $tmp;
                    $dcs         = $dc->where($dcon)->field('id')->select();

                    foreach ($dcs as $value) {
                        //将所有子节点压入等待堆栈
                        array_push($waitList, $value['id']);
                    }
                }
                //dump($rsList);
                $where['fid']=array('in',$rsList);
                $res=M('shop')->where($where)->select();
                //dump($res);




        //联表查询
        //$a=M('shop')->join("onethink_price on onethink_shop.id=onethink_price.sid")->select();
        // $b=M('cate')->where(array('fid'=>$fid))->select();
      
        // foreach($b as $v){
        //     $arr[]=$v['id'];
        // }
        // $c=implode(',',$arr);
        // $where['fid']=array('in',$c);

        // $b=M('shop')->where($where)->select();
        // dump($b);
        // //dump(M('shop')->getLastSql());
        // dump(M('shop')->getLastSql());
        // //dump($img);die;
         $this->assign('_list', $res);
        
        $this->meta_title = '商品';
        $this->display();
    }

    public function catalog_list(){
        $nickname       =   I('nickname');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['id|title']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$nickname.'%');
        }

        $list   = $this->lists('shop',$map,'id');
        
        //联表查询
        $a=M('shop')->join("onethink_price on onethink_shop.id=onethink_price.sid")->select();
        $b=M('cate')->where(array('fid'=>$fid))->select();
        //dump($b);
        foreach($b as $v){
            $arr[]=$v['id'];
        }
        $c=implode(',',$arr);
        $where['fid']=array('in',$c);
        $a=M('shop')->where($where)->select();
        
        //dump(M('shop')->getLastSql());
        //dump(M('shop')->getLastSql());
        //dump($img);die;
        $this->assign('_list', $a);
        
        $this->meta_title = '商品';
        $this->display();
    }
}